************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 15 (Treatment Effects on Risk and Social Preference)
* Last edited: 07 May 2021
************************************************

*************************************************
* Initial Set-up
*************************************************

	clear all
	set more off
	set seed 1

	do "$s/anderson_index_program.do"

	cap reghdfe, compile
	
	use "$d/riskandsocial_dataset.dta", clear
	
************************************************

	*** Social and Risk Preferences Table

	*** Panel A: Risk/Loss
	
	//Component Regressions - nonmonotonic preferences are excluded
	
	tempfile temp
	save `temp', replace
	
	foreach var in risk_switch_point loss_switch_point {
		preserve
		
	    if "`var'" == "risk_switch_point" drop if r_nonmonotonic == 1
		
	    if "`var'" == "loss_switch_point" drop if l_nonmonotonic == 1
		
		collapse (mean) `var' (max) female age treat_pool treat_nap, by(pid post_treatment)
		
		reshape wide `var', i(pid treat_pool treat_nap) j(post_treatment)
		
		* Standardize postline variable
		bysort pid: egen `var'_post_temp = mean(`var'1) if !mi(`var'0)
		bysort pid: egen `var'_post = mean(`var'_post_temp)
		sum `var'_post if treat_pool == 0 & treat_nap == 0
		gen `var'_post_mean = r(mean)
		gen `var'_post_sd = r(sd)
		gen `var'_post_std = (`var'_post - `var'_post_mean) / `var'_post_sd
		drop `var'_post_mean `var'_post_sd `var'_post_temp
		
		* Standardize baseline variable
		bysort pid: egen `var'_pre_temp = mean(`var'0) if !mi(`var'1)
		bysort pid: egen `var'_pre = mean(`var'_pre_temp)
		sum `var'_pre if treat_pool == 0 & treat_nap == 0
		gen `var'_pre_mean = r(mean)
		gen `var'_pre_sd = r(sd)
		gen `var'_pre_std = (`var'_pre - `var'_pre_mean) / `var'_pre_sd
		drop `var'_pre_mean `var'_pre_sd `var'_pre_temp
		
		* Regress
		rename `var'_pre_std composite_pre
		
		xi: reg `var'_post_std treat_pool treat_nap composite_pre `controls', robust cluster(pid)
		
		local pids_`var'_nm = string(e(N_clust))
		local obs_`var'_nm = string(e(N))
		
		lincom _b[treat_pool]
		local coef_ns_`var'_nm = string(r(estimate),"%9.2f")
		local se_ns_`var'_nm = string(r(se),"%9.2f")
		local p_ns_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[treat_nap]
		local coef_nap_`var'_nm = string(r(estimate),"%9.2f")
		local se_nap_`var'_nm = string(r(se),"%9.2f")
		local p_nap_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))

		*just to make p-value loop work
		local coef_value_`var'_nm = 0
		local se_value_`var'_nm = 0
		local p_value_`var'_nm = 0
			
		lincom _b[composite_pre]
		local coef_base_`var'_nm = string(r(estimate),"%9.2f")
		local se_base_`var'_nm = string(r(se),"%9.2f")
		local p_base_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
		rename composite_pre `var'_pre_std
		restore
	}

	*** Panel B: Social Preferences
	
	//Composite Regressions
	use `temp', clear
	
	preserve
	
	collapse (mean) d_send u_send t_send u_avg_receive t_avg_amountsent (max) female age treat_pool treat_nap, by(pid post_treatment)
	
	* Create standardized versions of postline variables
	foreach var in d_send u_send t_send u_avg_receive t_avg_amountsent {
		bysort pid: egen `var'_post_temp = mean(`var') if post_treatment == 1
		bysort pid: egen `var'_post = mean(`var'_post_temp)
		sum `var'_post if treat_pool == 0 & treat_nap == 0 & post_treatment == 1
		gen `var'_post_mean = r(mean)
		gen `var'_post_sd = r(sd)
		gen `var'_post_std = (`var'_post - `var'_post_mean) / `var'_post_sd
		drop `var'_post_mean `var'_post_sd `var'_post_temp `var'_post
	}
	
	* Create postline social preference index - positive value indicates more social preference
	gen composite = (d_send_post_std + u_send_post_std + t_send_post_std + u_avg_receive_post_std + t_avg_amountsent_post_std)/5
	
	* Create standardized versions of baseline variables
	foreach var in d_send u_send t_send u_avg_receive t_avg_amountsent {
		bysort pid: egen `var'_pre_temp = mean(`var') if post_treatment == 0
		bysort pid: egen `var'_pre = mean(`var'_pre_temp)
		sum `var'_pre if post_treatment == 0 & d_send_post != . // we only want to include the controls who get in the regression
		gen `var'_pre_mean = r(mean)
		gen `var'_pre_sd = r(sd)
		gen `var'_pre_std = (`var'_pre - `var'_pre_mean) / `var'_pre_sd
		drop `var'_pre_mean `var'_pre_sd `var'_pre_temp `var'_pre
	}
	
	//Component Regressions
	
	* Regressions for senders
	foreach var in d_send u_send t_send u_avg_receive t_avg_amountsent {
	 		
		* Regress
		rename `var'_pre_std composite_pre
		
		xi: reg `var'_post_std treat_pool treat_nap composite_pre `controls' if post_treatment == 1, robust cluster(pid)
		
		local pids_`var' = string(e(N_clust))
		local obs_`var' = string(e(N))
				
		lincom _b[treat_pool]
		local coef_ns_`var' = string(r(estimate),"%9.2f")
		local se_ns_`var' = string(r(se),"%9.2f")
		local p_ns_`var' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[treat_nap]
		local coef_nap_`var' = string(r(estimate),"%9.2f")
		local se_nap_`var' = string(r(se),"%9.2f")
		local p_nap_`var' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))

		*just to make p-value loop work
		local coef_value_`var' = 0
		local se_value_`var' = 0
		local p_value_`var' = 0
			
		lincom _b[composite_pre]
		local coef_base_`var' = string(r(estimate),"%9.2f")
		local se_base_`var' = string(r(se),"%9.2f")
		local p_base_`var' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
		rename composite_pre `var'_pre_std
		
	}
	
	restore
	
	* Regressions for recipients - nonmonotonic preferences are excluded
	foreach var in u_receive t_receive{ //regressions for recipients are different from those for senders due to an extra control for amount of money recipients received
	 
	 preserve
	
	    if "`var'" == "u_receive" drop if u_nonmonotonic == 1
		
	    if "`var'" == "t_receive" drop if t_nonmonotonic == 1
		
	    * Reshape dataset from wide to long
		gen id = _n
		reshape long `var', i(id) j(value)
		
		collapse (mean) `var' (max) female age treat_pool treat_nap, by(pid post_treatment value)
		
		reshape wide `var', i(pid value treat_pool treat_nap) j(post_treatment)
		
		* Standardize postline variable
		bysort pid: egen `var'_post_temp = mean(`var'1) if !mi(`var'0)
		bysort pid: egen `var'_post = mean(`var'_post_temp)
		sum `var'_post if treat_pool == 0 & treat_nap == 0
		gen `var'_post_mean = r(mean)
		gen `var'_post_sd = r(sd)
		gen `var'_post_std = (`var'1 - `var'_post_mean) / `var'_post_sd
		drop `var'_post_mean `var'_post_sd `var'_post_temp
		
		* Standardize baseline variable
		bysort pid: egen `var'_pre_temp = mean(`var'0) if !mi(`var'1)
		bysort pid: egen `var'_pre = mean(`var'_pre_temp)
		sum `var'_pre if treat_pool == 0 & treat_nap == 0
		gen `var'_pre_mean = r(mean)
		gen `var'_pre_sd = r(sd)
		gen `var'_pre_std = (`var'_pre - `var'_pre_mean) / `var'_pre_sd
		drop `var'_pre_mean `var'_pre_sd `var'_pre_temp
		
		* Standardize value
		qui sum value 
		gen value_mean = r(mean)
		gen value_sd = r(sd)
		gen value_std = (value - value_mean) / value_sd
		
		* Regress
		rename `var'_pre_std composite_pre
	
		xi: reg `var'_post_std treat_pool treat_nap value_std composite_pre `controls', robust cluster(pid)
		
		local pids_`var'_nm = string(e(N_clust))
		local obs_`var'_nm = string(e(N))
		
		lincom _b[treat_pool]
		local coef_ns_`var'_nm = string(r(estimate),"%9.2f")
		local se_ns_`var'_nm = string(r(se),"%9.2f")
		local p_ns_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
		
		lincom _b[treat_nap]
		local coef_nap_`var'_nm = string(r(estimate),"%9.2f")
		local se_nap_`var'_nm = string(r(se),"%9.2f")
		local p_nap_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))

		lincom _b[value_std]
		local coef_value_`var'_nm = string(r(estimate),"%9.2f")
		local se_value_`var'_nm = string(r(se),"%9.2f")
		local p_value_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
			
		lincom _b[composite_pre]
		local coef_base_`var'_nm = string(r(estimate),"%9.2f")
		local se_base_`var'_nm = string(r(se),"%9.2f")
		local p_base_`var'_nm = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
						
		rename composite_pre `var'_pre_std	
		restore
	}

		* Significance
		foreach var in risk_switch_point_nm loss_switch_point_nm d_send u_send t_send u_avg_receive t_avg_amountsent {
			foreach j in ns nap value{
				 if `p_`j'_`var'' < 0.01 {
					local coef_`j'_`var' = "`coef_`j'_`var''***"
				}
				
				if `p_`j'_`var'' < 0.05 & `p_`j'_`var'' >= 0.01 {
					local coef_`j'_`var' = "`coef_`j'_`var''**"
				}
				
				 if `p_`j'_`var'' < 0.1 & `p_`j'_`var'' >= 0.05 {
					local coef_`j'_`var' = "`coef_`j'_`var''*"
				}
			}
		}

		** Exporting Table **
		
	cd "$oa/Tables"
		file open f using "TableA15_risk_social_pref.tex", write replace
		file write f "\begin{tabular}{l*{7}{c}}" _n ///+
		"\toprule" _n ///
		"&\multicolumn{2}{c}{\textbf{Risk Preferences}}&\multicolumn{5}{c}{\textbf{Social Preferences}}\\" _n ///
		"\cmidrule(lr){2-3}\cmidrule(lr){4-8}" _n ///
		"&\multicolumn{1}{p{2cm}}{\centering  Risk \\ Aversion \\ (1)} & \multicolumn{1}{p{2cm}}{\centering Loss \\ Aversion \\ (2)} & \multicolumn{1}{p{2cm}}{\centering Dictator \\ Send \\ (3)}& \multicolumn{1}{p{2cm}}{\centering  Ultimatum \\ Send \\ (4)} & \multicolumn{1}{p{2cm}}{\centering  Trust \\ Send \\ (5)} & \multicolumn{1}{p{2cm}}{\centering  Ultimatum \\ Receive \\ (6)} & \multicolumn{1}{p{3cm}}{\centering  Trust \\ Send Back \\ (7)}\\" _n ///
		"\midrule" _n ///
		"Night-Sleep Treatments & `coef_ns_risk_switch_point_nm' & `coef_ns_loss_switch_point_nm' & `coef_ns_d_send' & `coef_ns_u_send' & `coef_ns_t_send' & `coef_ns_u_avg_receive' & `coef_ns_t_avg_amountsent' \\" _n ///
		"& (`se_ns_risk_switch_point_nm') & (`se_ns_loss_switch_point_nm') & (`se_ns_d_send') & (`se_ns_u_send') & (`se_ns_t_send') & (`se_ns_u_avg_receive') & (`se_ns_t_avg_amountsent') \\" _n ///
		"Nap Treatment & `coef_nap_risk_switch_point_nm' & `coef_nap_loss_switch_point_nm' & `coef_nap_d_send' & `coef_nap_u_send' & `coef_nap_t_send' & `coef_nap_u_avg_receive' & `coef_nap_t_avg_amountsent' \\" _n ///
		"& (`se_nap_risk_switch_point_nm') & (`se_nap_loss_switch_point_nm')  & (`se_nap_d_send') & (`se_nap_u_send') & (`se_nap_t_send') & (`se_nap_u_avg_receive') & (`se_nap_t_avg_amountsent') \\" _n ///
		"\midrule" _n ///
		"Participants & `pids_risk_switch_point_nm'  & `pids_loss_switch_point_nm' & `pids_d_send'  & `pids_u_send'  & `pids_t_send'  & `pids_u_avg_receive'  & `pids_t_avg_amountsent' \\" _n ///
		"\bottomrule \\" _n ///
		"\end{tabular}" _n
	file close f
